package com.bingo.dbutils;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

import com.bingo.jdbc.JdbcUtils;
import com.mchange.v2.c3p0.impl.NewPooledConnection;

public class Demo2 {
	@Test
	public void fun1(){
		Stu stu = new Stu(1002,"bingo",30,"male");
		addStu(stu);
		
		Stu s = load(1002);
		System.out.println(s);
	}
	public void addStu(Stu stu){
		QR qr = new QR(JdbcUtils.getDataSource());
		String sql = "insert into t_stu values(?,?,?,?)";
		//给出参数
		Object[] params = {stu.getSid(),stu.getSname(),stu.getAge(),stu.getGender()};
		//调用增、删、改
		qr.update(sql, params);

	}
	public Stu load(int sid){
		QR qr = new QR(JdbcUtils.getDataSource());
		String sql = "select * from t_stu where sid=?";
		Object[] params = {sid};
		
		RsHandler<Stu> rh = new RsHandler<Stu>() {
			@Override
			public Stu handle(ResultSet rs) throws SQLException {
				// TODO Auto-generated method stub
				if(!rs.next()) return null;
				Stu stu = new Stu();
				stu.setSid(rs.getInt("sid"));
				stu.setSname(rs.getString("sname"));
				stu.setAge(rs.getInt("age"));
				stu.setGender(rs.getString("gerder"));
				return stu;
			}
		};
		return (Stu) qr.query(sql, rh, params);
	}
}
